package base.IO流;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class 排序字符文本 {
    public static void main(String[] args) throws IOException {
        /*1.读取数据*/
        File scr = new File(".\\src\\base\\IO流\\a.txt");
        FileReader fr = new FileReader(scr);
        StringBuilder sbu = new StringBuilder();
        int i;
        while((i = fr.read()) != -1){
            sbu.append((char) i);
        }
        String str = sbu.toString();
        Pattern p = Pattern.compile("\\d+(?:-\\d+)+");
        Matcher m = p.matcher(str);
        String s = null;
        while(m.find()){
            s = m.group();
        }
        System.out.println(s);

        /*2.排序*/
        String[] arrstr = s.split("-");
        ArrayList<Integer> list = new ArrayList<>();
        for (int j = 0; j < arrstr.length; j++) {
            int num = Integer.parseInt(arrstr[j]);
            list.add(num);
        }
        Collections.sort(list);
        System.out.println(list);

        /*3.写出*/
        FileWriter fw = new FileWriter(scr,true);
        fw.write("\n");
        for (int j = 0; j < list.size(); j++) {
            if (j == list.size()-1){
                fw.write(list.get(j)+"");
            }else {
                fw.write(list.get(j)+"-");
            }
        }
        fw.close();
        fr.close();
    }
}
